//
//  TravelsDetailVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/8/5.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "TravelsDetailVC.h"
#import "TravelsComListVC.h"
@interface TravelsDetailVC ()<UIGestureRecognizerDelegate,UITextViewDelegate>

@end

@implementation TravelsDetailVC
{
    UIView *_comView;
    UITextView *_comTV;
    UILabel *_placeHolderLabel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = White_Color;
    self.title = self.travelsName;
    
    UIWebView *_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 66, KScreenWidth, KScreenHeight-40-66)];
    NSString *str = [NSString readToNSDefaultsWithKey:kType];
    if ([str isEqual:@"0"]) {
        //0为俱乐部用户
        [_webView setFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    }else{
        UIView *commentView = [[UIView alloc]initWithFrame:CGRectMake(0, KScreenHeight-40, KScreenWidth, 40)];
        commentView.backgroundColor = superBackgroundColor;
        UIButton *editComBtn = [[UIButton alloc]initWithFrame:CGRectMake(8, 5, KScreenWidth-8-8-28, 30)];
        editComBtn.layer.borderWidth = 1;
        editComBtn.layer.borderColor = LightGray_Color.CGColor;
        editComBtn.layer.cornerRadius = 8;
        [editComBtn setTitle:@"写评论..." forState:UIControlStateNormal];
        [editComBtn setTitleColor:Gray_Color forState:UIControlStateNormal];
        editComBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        editComBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        [editComBtn addTarget:self action:@selector(editComBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [commentView addSubview:editComBtn];
        UIButton *comListBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth-28, 10, 20, 20)];
        [comListBtn setImage:[UIImage imageNamed:@"comment"] forState:UIControlStateNormal];
        [comListBtn addTarget:self action:@selector(comListBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [commentView addSubview:comListBtn];
        [self.view addSubview:commentView];
    }
    _webView.scrollView.contentInset = UIEdgeInsetsMake(70, 0, 0, 0);
    _webView.backgroundColor = [UIColor clearColor];
    _webView.opaque = NO;
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, -70, KScreenWidth, 70)];
    headerView.backgroundColor = superBackgroundColor;
    //游记headerView内容
    //标题
    UILabel *titleLb = [[UILabel alloc]initWithFrame:CGRectMake(8, 8, KScreenWidth-16, 20)];
    titleLb.text = self.travelsName;
    titleLb.textColor = Theme_Color;
    [headerView addSubview:titleLb];
    //头像
    UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake(8, 36, 25, 25)];
    [icon sd_setImageWithURL:[NSURL URLWithString:self.travelIcon] placeholderImage:[UIImage imageNamed:@"userIcon"]];
    icon.layer.cornerRadius = 12.5;
    icon.layer.masksToBounds = YES;
    [headerView addSubview:icon];
    //作者
    UILabel *authorLb = [[UILabel alloc]initWithFrame:CGRectMake(43, 38, KScreenWidth/2-43, 20)];
    authorLb.text = self.travelAuthor;
    authorLb.textColor = Theme_Color;
    authorLb.font = [UIFont systemFontOfSize:12];
    [headerView addSubview:authorLb];
    //时间
    UILabel *timeLb = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth/2, 38, KScreenWidth/2-8, 20)];
    timeLb.text = self.travelTime;
    timeLb.textAlignment = NSTextAlignmentRight;
    timeLb.font = [UIFont systemFontOfSize:12];
    timeLb.textColor = Gray_Color;
    [headerView addSubview:timeLb];
    [_webView.scrollView addSubview:headerView];
    
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerIncident:)];
    singleFingerOne.delegate = self;
    singleFingerOne.numberOfTouchesRequired = 1;//手指数
    singleFingerOne.numberOfTapsRequired = 1;//点击次数
    [_webView addGestureRecognizer:singleFingerOne];
    _webView.backgroundColor = White_Color;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.htmlUrlStr]];
    [_webView loadRequest:request];
    
    [self.view addSubview:_webView];
    
//    [self requsetForAddComment];
//    [self requsetForDeleComment];
//    [self requestForReplyComment];
//    [self requsetForCommentList];
}
- (void)editComBtnAction{
    _comView = [[UIView alloc]initWithFrame:CGRectMake(0, KScreenHeight-140, KScreenWidth, 140)];
    _comView.backgroundColor = blackLineColore;
    _comTV = [[UITextView alloc]initWithFrame:CGRectMake(5, 10, KScreenWidth - 10, 130)];
    [_comTV becomeFirstResponder];
    _comTV.delegate = self;
    _comTV.layer.borderColor = blackLineColore.CGColor;
    _comTV.layer.borderWidth = 1;
    _comTV.layer.cornerRadius = 5;
    _comTV.font = [UIFont systemFontOfSize:16];
    //placeHolderLabel
    _placeHolderLabel = [[UILabel alloc]initWithFrame:CGRectMake(3, 3, 300, 30)];
    if (_comTV.text.length == 0) {
        _placeHolderLabel.text = @"输入评论内容";
    }else{
        _placeHolderLabel.text = @"";
    }
    _placeHolderLabel.font = [UIFont systemFontOfSize:16];
    [_placeHolderLabel setTextColor:LightGray_Color];
    [_comTV addSubview:_placeHolderLabel];
    [_comView addSubview:_comTV];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(5, 110, KScreenWidth-10, 30)];
    view.backgroundColor = blackLineColore;
    [_comView addSubview:view];
    UIButton *sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth-60, 115, 60, 20)];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:Theme_Color forState:UIControlStateNormal];
    [sendBtn addTarget:self action:@selector(comBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_comView addSubview:sendBtn];
    [self.view addSubview:_comView];
}
#pragma - mark UITextViewDelegate
-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        _placeHolderLabel.text = @"输入评论内容";
    }else{
        _placeHolderLabel.text = @"";
    }
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer{
    return YES;
}
- (void)fingerIncident:(UITapGestureRecognizer *)sender{
    _comView.hidden = YES;
}
- (void)requsetForAddComment{
    
}
- (void)requsetForDeleComment{
    //参数
    NSDictionary *parameters = @{
                                 @"commentId":@"1",
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/comment/delete.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        NSDictionary *_responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",_responseDic);
        //        NSArray *_responseArr = _responseDic[@"list"];
        NSString *error = _responseDic[@"code"];
        if (![_responseDic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
            
        }else{
            //请求成功
            [SVProgressHUD showSuccessWithStatus:@"删除评论成功"];
            //            [self initCollectionView];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"删除评论失败"];
    }];
}
- (void)requestForReplyComment{
    //参数
    NSDictionary *parameters = @{
                                 @"objID":@"2",
                                 @"objType": @"2",
                                 @"commType": @"1",
                                 @"originalID":self.travelsId,
                                 @"comm": @"很不错哦改改改改改",
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/comment/reply.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        NSDictionary *_responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",_responseDic);
        //        NSArray *_responseArr = _responseDic[@"list"];
        NSString *error = _responseDic[@"code"];
        if (![_responseDic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
            
        }else{
            //请求成功
            [SVProgressHUD showSuccessWithStatus:@"回复评论成功"];
            //            [self initCollectionView];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"回复评论失败"];
    }];
}
//
- (void)comBtnAction{
    //参数
    NSDictionary *parameters = @{
                                 @"objID":self.travelsId,
                                 @"objType": @"1",
                                 @"commType": @"1",
                                 @"comm": _comTV.text,
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/comment/create.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        NSDictionary *_responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",_responseDic);
        //        NSArray *_responseArr = _responseDic[@"list"];
        NSString *error = _responseDic[@"code"];
        if (![_responseDic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
            
        }else{
            //请求成功
            [SVProgressHUD showSuccessWithStatus:@"评论成功"];
            [self.view endEditing:YES];
            _comView.hidden = YES;
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"评论失败"];
    }];
}
- (void)comListBtnAction{
    TravelsComListVC *vc = [[TravelsComListVC alloc]init];
    vc.travelsId = self.travelsId;
    [self.view endEditing:YES];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
